package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.FreightTemplateChargingDAO;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplate;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplateCharging;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplateCharging.Column;
import com.ecoolex.chinadutyfree.retailer.entity.FreightTemplateChargingExample;
import com.ecoolex.chinadutyfree.retailer.pojo.EditFreightTemplateChargingDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;
import com.google.common.collect.Lists;


@Service
public class FreightTemplateChargingManager {

	@Autowired
	private FreightTemplateChargingDAO dao;

	@Autowired
	private FreightTemplateChargingRegionManager chargingRegionManager;

	private void verify(FreightTemplateCharging record) {
		BizAssert.isTrue(Check.notNullAndGteZero(record.getBasicNumber()), ResultCode.VERIFY.build(30), "基础计数为空");
		BizAssert.isTrue(Check.notNullAndGteZero(record.getBasicCharging()), ResultCode.VERIFY.build(31), "基础计费为空");
		BizAssert.isTrue(Check.notNullAndGtZero(record.getRenewNumber()), ResultCode.VERIFY.build(32), "继续计数为空或者0");
		BizAssert.isTrue(Check.notNullAndGteZero(record.getRenewCharging()), ResultCode.VERIFY.build(33), "继续计费为空");
	}

	public void update(FreightTemplate template, List<EditFreightTemplateChargingDTO> chargingList, String updateUser) {
		// 已经入库的数据
		List<FreightTemplateCharging> oldList = queryByFreightTemplateId(template.getFreightTemplateId());

		chargingList.forEach(record -> {
			verify(record);

			if (Check.notNull(record.getChargingId())) {
				// 检查一下是否存在
				FreightTemplateCharging old = oldList.stream().collect(
					CollectorUtil.first((old2 -> old2.getChargingId().equals(record.getChargingId()))));
				// 验证
				BizAssert.isTrue(Check.notNull(old),
					ResultCode.BIZ.build(1),
					"ChargingId={},不存在该运费模板计费",
					record.getChargingId());
				// 修改值
				BeanUtil.copy(record,
					old,
					Lists.newArrayList(Column.freightTemplateId,
						Column.deleted,
						Column.createUser,
						Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
				old.setUpdateUser(updateUser);
				old.setUpdateTime(Date.from(Instant.now()));
				// 修改
				dao.updateByPrimaryKeySelective(old,
					Column.excludes(Column.freightTemplateId, Column.deleted, Column.createUser, Column.createTime));
				// 绑定地区
				chargingRegionManager.update(old, record.getRegionList(), updateUser);
				// 确定修改的,就移除掉
				oldList.remove(old);
			} else {
				record.setFreightTemplateId(template.getFreightTemplateId());
				record.setDeleted(FreightTemplate.NOT_DELETED);
				record.setCreateUser(updateUser);
				record.setCreateTime(Date.from(Instant.now()));
				record.setUpdateUser(updateUser);
				record.setUpdateTime(Date.from(Instant.now()));
				dao.insert(record);
				// 绑定地区
				chargingRegionManager.update(record, record.getRegionList(), updateUser);
			}

		});
		// 删除没有被传进来的数据
		oldList.forEach(old -> dao.deleteByPrimaryKey(old.getChargingId()));
	}

	public void deleteByFreightTemplate(FreightTemplate template, String updateUser) {
		FreightTemplateCharging record = new FreightTemplateCharging();
		record.setDeleted(FreightTemplateCharging.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));

		FreightTemplateChargingExample example = new FreightTemplateChargingExample();
		example.createCriteria().andFreightTemplateIdEqualTo(template.getFreightTemplateId()).andDeletedEqualTo(
			FreightTemplateCharging.NOT_DELETED);
		dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);

		chargingRegionManager.deleteByFreightTemplate(template, updateUser);
	}

	public List<FreightTemplateCharging> queryByFreightTemplateId(Integer freightTemplateId) {
		FreightTemplateChargingExample example = new FreightTemplateChargingExample();
		example.createCriteria().andFreightTemplateIdEqualTo(freightTemplateId).andDeletedEqualTo(
			FreightTemplateCharging.NOT_DELETED);
		return dao.selectByExample(example);
	}

	public FreightTemplateCharging getByFreightTemplateIdAndId(Integer freightTemplateId, Integer chargingId) {
		FreightTemplateChargingExample example = new FreightTemplateChargingExample();
		example.createCriteria().andChargingIdEqualTo(chargingId).andFreightTemplateIdEqualTo(
			freightTemplateId).andDeletedEqualTo(FreightTemplateCharging.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

	public FreightTemplateCharging getById(Integer chargingId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(chargingId, FreightTemplateCharging.NOT_DELETED);
	}
}